<template>
  <div>
    <!--      搜索组件-->
    <SearchModle dataKey="tearch" @searchFn="searchFn"></SearchModle>
    <!--      end-->
    <div class="mb-10">
      <div class="toplineBox">
        <div class="toplineBox-left">
          <el-button @click="handleEdit()" size="small">新增职工</el-button>
          <!--            <el-button type="danger" size="small" :disabled="handleSelection.length==0?true:false"-->
          <!--                       @click="detelTearch(2)">-->
          <!--              批量离职教工-->
          <!--            </el-button>-->
          <el-button size="small" @click="gotoHistory">教职工离职记录</el-button>
          <downexcel type="tearchList"></downexcel>
        </div>
        <p class="numberline toplineBox-right">总{{ page.total }}条</p>
      </div>
    </div>
    <!--本体列表，数据 -->
    <div class="table-body">
      <div class="table-body-box">
        <el-table
          border
          ref="multipleTable"
          :data="arryList"
          :stripe="tableStyle.tableStripe"
          :border="tableStyle.tableBorder"
          tooltip-effect="dark"
          style="width: 100%"
          header-cell-class-name="table-text-c"
          cell-class-name="table-text-c"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="50"
            align="center">
          </el-table-column>
          <el-table-column
            prop="id"
            label="ID"
            width="90"
            align="center">
          </el-table-column>
          <el-table-column
            align="center"
            prop="account"
            label="账户名"
            width="110">
          </el-table-column>
          <el-table-column
            align="center"
            prop="realname"
            label="姓名">
          </el-table-column>
          <el-table-column
            label="头像"
            width="80"
            align="center">
            <template slot-scope="scope">
              <!--                <viewerModle type="1" :imgSrc="scope.row.pic"></viewerModle>-->
              <ImagePreview :src="scope.row.pic" width="40px" height="40px"></ImagePreview>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="section_name"
            label="年段">
            <template slot-scope="scope">
              <span v-if="scope.row.section">{{ scope.row.section.section_name }}</span>
              <span v-if="scope.row.classes">（{{ scope.row.classes.class_name }}）</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="role"
            label="角色"
            align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.role">{{ scope.row.role.name }}</span>
              <span v-else class="notborrow">未定义</span>
            </template>
          </el-table-column>
          <el-table-column
            label="归属"
            align="center">
            <template slot-scope="scope">
              {{schoolData[scope.row.school_id] && schoolData[scope.row.school_id].name}}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center">
            <template slot-scope="scope">
              <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button size="small" @click="tearcherClass(scope.row)">班级变更</el-button>
              <el-button size="small" @click="workerId=scope.row.id;agentVisible=true">代理班级</el-button>
              <el-button size="small" @click="changePassWordFn(scope.row)" v-if="userInfo && userInfo.id==1">密码重置
              </el-button>
              <el-button size="small" @click="detelTearch(1,scope.$index)" type="danger">离职</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!--工具条 分页-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        background
        layout="total, prev, pager, next"
        :current-page="page.pageindex"
        :total="page.total"
        :page-size="page.pagesize"
        @current-change="handleSizeChange">
      </el-pagination>
    </el-col>
    <addUser :visible.sync="dialogVisible" @getevents="getevents" :eidtOption="eidtOption"
             :checkType="checkType"></addUser>
    <tearchGetClass :visible.sync="tearchVisible" :tearchOption="tearchOption" @InitFn="changeList"></tearchGetClass>
    <!--    编辑添加教职工-->
    <teachPopup :visible.sync="tearchEditVisible" :tearchOption="tearchOption" @InitFn="InitFn"></teachPopup>
    <teachHistory :visible.sync="historyVisible"></teachHistory>
    <agentClass :visible.sync="agentVisible" :workerId="workerId"></agentClass>
  </div>
</template>

<script>
import addUser from '@/components/dialog/addUser.vue'
import tearchGetClass from '@/components/dialog/tearchGetClass.vue'
import teachHistory from "../teachHistory";
import teachPopup from "../teachPopup";
import {pagination, tableoption} from '@/utils/util.js'
import api from '@/utils/api.js'
import {resetPassWord} from '@/api/admin/users.js'
import {mapActions} from "vuex";
import agentClass from "./agentClass";
import {schoolIndex} from '@/api/admin/system'

export default {
  components: {
    addUser,
    tearchGetClass,
    teachPopup,
    teachHistory,
    agentClass
  },
  data() {
    return {
      // 设置table样式
      tableStyle: tableoption,
      tearchVisible: false,
      dialogVisible: false,
      arryList: [], // 数据列表
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      handleSelection: [],
      // new
      eidtOption: null,
      checkType: null,
      tearchOption: '',
      // 搜索组件
      searchForm: {},
      tearchEditVisible: false,
      historyVisible: false,
      agentVisible: false,
      workerId: 0,
      userInfo: null,
      schoolData: {}
    }
  },
  created() {
    this.schoolIndexFn()
    this.GetInfo().then(res => {
      this.userInfo = res.detail
    })
    this.InitFn()
  },
  methods: {
    ...mapActions(['GetInfo']),
    // 初始化
    InitFn() {
      this.searchForm.p = this.page.pageindex
      this.$fetch(api.admin_user_index, this.searchForm).then((response) => {
        if (response.code === 0) {
          this.arryList = response.list.data
          this.page.total = response.list.total
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 搜索组件
    searchFn(option) {
      console.log('option:', option)
      this.page.pageindex = 1
      this.searchForm = option
      this.InitFn()
    },
    changeList() {
      this.InitFn()
    },
    tearcherClass(option) {
      this.tearchOption = option
      this.tearchVisible = true
    },
    // 分页
    handleSizeChange(val) {
      this.arryList = []
      this.page.pageindex = val
      this.InitFn()
    },
    handleSelectionChange(val) {
      console.log('选择框选择变化', val) // 所有选中值
      this.handleSelection = val
    },
    detelTearch(type, index) {
      let name = ''
      // type 1 删除单个 2 批量删除
      if (type === 1) {
        name = this.arryList[index].realname
      } else {
        for (let i in this.handleSelection) {
          name += (this.handleSelection[i].realname + ',')
        }
      }
      this.$confirm('确认 ' + name + ' 该教工离职, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.detelRespon(type, index)
      }).catch(() => {
      })
    },
    detelRespon(type, index) {
      // type 1 单独 2 批量
      console.log(type, index)
      let option_ = []
      let data_ = {}
      if (type === 2) {
        for (let i in this.handleSelection) {
          option_.push(this.handleSelection[i].id)
        }
        data_.id = option_.join(',')
      } else {
        data_.id = this.arryList[index].id
      }
      console.log('删除数据：', data_)
      this.$post(api.admin_user_delete, data_).then((response) => {
        if (response.code === 0) {
          this.InitFn()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 编辑教职工
    handleEdit(row) {
      this.tearchOption = row ? JSON.parse(JSON.stringify(row)) : ''
      this.tearchEditVisible = true
      // let id = row || ''
      // this.$router.push('/tearchDetail?editId=' + id)
    },
    getevents() {
      this.page.pageindex = 1
      this.InitFn()
    },
    gotoHistory() {
      console.log('返回到离职教职工列表')
      this.historyVisible = true;
    },
    deletWx(index) {
      let n = this;
      this.$confirm('确认解除' + this.arryList[index].realname + '微信绑定, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.closeBinding(index, n.arryList[index].id)
      }).catch(() => {
      })
    },
    closeBinding(index, id) {
      let n = this;
      this.$post(this.api.tearch.closeWxBind, {id: id}).then((response) => {
        if (response.code === 0) {
          n.arryList[index].openid = ''
          this.$message({
            type: 'success',
            message: '解绑成功!'
          })
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 重置密码
    changePassWordFn(row) {
      this.$confirm('确认重置该账户密码，重置后密码：s2d0x1, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.changePass(row)
      }).catch(() => {
      })
    },
    async changePass(row) {
      resetPassWord({id: row.id}).then(res => {
        if (!res.code) {
          this.$message.success('重置成功！')
        }
      })
    },
    // 代理班级
    agentClass() {

    },
    schoolIndexFn() {
      schoolIndex().then(res => {
        let schoolData_ = {}
        for (let item of res.list) {
          schoolData_[item.id] = item
        }
        this.schoolData = schoolData_
      })
    },
  },
  mounted() {
  }
}
</script>
<style scoped>

.wx-user-box-line img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  margin-right: 5px;
}

.wx-user-box-line {
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-wx {
  cursor: pointer;
  padding: 5px;
}

.btn-wx:hover {
  color: #1b8de5;
  text-decoration: underline;
}

.delet-wx {
  color: red;
  cursor: pointer;
}

.delet-wx:hover {
  text-decoration: underline;
}
</style>
<!--<div class="wx-user-box">-->
<!--<span>{{scope.row.openid?'已绑定':''}}</span>-->
<!--<span v-if="scope.row.openid" class="wx-user-box-line">-->
<!--                    <img :src="scope.row.wx.pic" alt="" width="30" height="30">{{scope.row.wx.user_name}}-->
<!--                  </span>-->
<!--</div>-->
